package com.gears_software.sxdc.dccx.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;

import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.TimePickerView;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.model.Cities;
import com.gears_software.sxdc.dccx.data.model.LineInfoModel;
import com.gears_software.sxdc.dccx.data.sharedPreference.DCCXSp_;
import com.gears_software.sxdc.dccx.ui.util.ShowTime;
import com.jaeger.library.StatusBarUtil;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.sharedpreferences.Pref;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import qiu.niorgai.StatusBarCompat;


/**
 * Created by SunMuran on 2017/7/22.
 * 选择路线页面
 */
@EActivity(R.layout.selector_line)
public class SelectorLineActivity extends Activity {
    @App
    DCCXApplication app;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @ViewById
    TextView tv_icon;
    @ViewById
    TextView tv_chufadi;
    @ViewById
    TextView tv_mudidi;
    @ViewById
    RelativeLayout rl_date;
    @ViewById
    TextView tv_date;
    @Pref
    DCCXSp_ sp;

    private TimePickerView pvTime;

    LineInfoModel lineInfoModel;

    @AfterViews
    void created() {
        actionbar_title.setText("选择路线");
        initIconfont();
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorActionbar));
    }

    void initIconfont() {
        tv_icon.setTypeface(app.iconfont);
        back_icon.setTypeface(app.iconfont);
        back_icon.setText(R.string.back_icon);
        right_icon.setVisibility(View.GONE);
    }

    //点出发地的时候跳转
    @Click
    void re_chufadi() {
        Intent intent = new Intent(this, SelectorCitiesActivity_.class);
        startActivityForResult(intent, 1);
    }

    //点目的地的时候跳转
    @Click
    void re_mudidi() {
        Intent intent = new Intent(this, SelectorCitiesActivity_.class);
        startActivityForResult(intent, 1);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            lineInfoModel = (LineInfoModel) data.getExtras().get("cities");
            if (requestCode == 1) {
                tv_chufadi.setText(lineInfoModel.getStartpoint());
                tv_mudidi.setText(lineInfoModel.getEndpoint());

            }
        }
    }

    //点中间的按钮时两个城市互换
    int flag = 0;

    @Click
    void tv_icon() {
        if (tv_chufadi.getText().toString().equals("") || tv_mudidi.getText().toString().equals("")) {
            TastyToast.makeText(this, "末选择出发地或目的地", app.SHORT, app.WARNING);
        } else {
            switch (flag) {
                case 0:
                    String chufadi = tv_chufadi.getText().toString();
                    String mudidi = tv_mudidi.getText().toString();
                    tv_chufadi.setText(mudidi);
                    tv_mudidi.setText(chufadi);
                    lineInfoModel.setStartpoint(mudidi);
                    lineInfoModel.setEndpoint(chufadi);
                    lineInfoModel.setDirection(2);
                    flag = 1;
                    break;
                case 1:
                    String chufadi1 = tv_chufadi.getText().toString();
                    String mudidi1 = tv_mudidi.getText().toString();
                    tv_mudidi.setText(chufadi1);
                    tv_chufadi.setText(mudidi1);
                    lineInfoModel.setStartpoint(mudidi1);
                    lineInfoModel.setEndpoint(chufadi1);
                    lineInfoModel.setDirection(1);
                    flag = 0;
                    break;
            }
        }

    }

    @Click
    void rl_date() {
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        startDate.set(startDate.get(Calendar.YEAR), startDate.get(Calendar.MONTH), startDate.get(Calendar.DAY_OF_MONTH));
        Calendar endDate = Calendar.getInstance();
        endDate.set(2020, 11, 1);
        pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调

                tv_date.setText(getTime(date));
            }
        })
                //年月日时分秒 的显示与否，不设置则默认全部显示
                .setType(new boolean[]{true, true, true, false, false, false})
                .setLabel("年", "月", "日", "", "", "")
                .isCenterLabel(true)
                .setCancelText("取消")
                .setSubmitText("确认")//确认按钮文字
                .setDividerColor(Color.DKGRAY)
                .isCyclic(true)
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setDecorView(null)
                .setLineSpacingMultiplier(2)
                .build();

        pvTime.show();
    }

    private String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String str = format.format(date);
        return str;
    }

    @Click
    void btn_confrim() {
        if ((!tv_chufadi.getText().toString().equals("") || !tv_mudidi.getText().toString().equals("")) || !(tv_date.getText().toString().equals(""))) {
            if (lineInfoModel != null) {
                if (lineInfoModel.getStartpoint() != null && !lineInfoModel.getStartpoint().equals("")
                        && lineInfoModel.getEndpoint() != null && !lineInfoModel.getEndpoint().equals("")) {
                    sp.startPoint().put(lineInfoModel.getStartpoint());
                    sp.endPorint().put(lineInfoModel.getEndpoint());
                    sp.direction().put(lineInfoModel.getDirection());
                }
            }
            sp.departtime().put(tv_date.getText().toString());
            setResult(1);
            finish();
        } else {
            TastyToast.makeText(this, "请选择线路或者出发时间", app.SHORT, app.WARNING);

        }

    }

    @Click
    void back_icon() {
        this.finish();
    }
}
